package com.yantong.controller.product;

import com.yantong.annotation.Log;
import com.yantong.controller.BaseController;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.MemberProductAuthority;
import com.yantong.pojo.ProductAuthority;
import com.yantong.service.MemberProductAuthorityService;
import com.yantong.service.ProductAuthorityService;
import com.yantong.utils.ValidateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  商品权限控制器
 *
 * @author huangshuai-728982210@qq.com
 * @date 2021/5/28 15:29
 */
@Controller
@RequestMapping("/product/productAuthority")
public class ProductAuthorityController extends BaseController {
	@Autowired
	private ProductAuthorityService productAuthorityService;
	private static final String prefix = "product/productAuthority";

	@Autowired
	private MemberProductAuthorityService memberProductAuthorityService;


	/**
	 * 页面
	 *
	 * @return
	 */
	@GetMapping()
	@RequiresPermissions("product:productAuthority:productAuthority")
	public String productAuthority(Model model) {

		return prefix + "/productAuthority";
	}

	/**
	 * 列表数据
	 *
	 * @param productAuthority
	 * @param offset
	 * @param limit
	 * @return
	 */
	@GetMapping("/list")
	@RequiresPermissions("product:productAuthority:productAuthority")
	@ResponseBody
	public PageUtils<ProductAuthority> listCentreInDustry(ProductAuthority productAuthority, int offset, int limit) {
		return productAuthorityService.listProductAuthority(productAuthority, offset, limit);
	}

	/**
	 * 编辑页面
	 * @param productAuthorityId
	 * @param model
	 * @return
	 */
	@GetMapping("/edit/{productAuthorityId}")
	@RequiresPermissions("product:productAuthority:edit")
	public String edit(@PathVariable Long productAuthorityId, Model model) {
		model.addAttribute("productAuthority", productAuthorityService.getProductAuthorityById(productAuthorityId));
		return prefix + "/edit";
	}

	/**
	 * 编辑操作
	 * @param productAuthority
	 * @return
	 */
	@Log(module = "商品权限管理", operation = "编辑商品权限")
	@ResponseBody
	@RequiresPermissions("product:productAuthority:edit")
	@PostMapping("/update")
	public ResultVo update(ProductAuthority productAuthority) {
		if (productAuthority == null || productAuthority.getId() == null) {
			return ResultVo.error();
		}
		if (productAuthorityService.updateProductAuthority(productAuthority) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	@RequiresPermissions("product:productAuthority:add")
	@GetMapping("/add")
	public String add(Model model) {
		return prefix +"/add";
	}


	@Log(module = "商品权限管理", operation = "添加商品权限")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("product:productAuthority:add")
	public ResultVo saveRate(ProductAuthority productAuthority) {
		if (productAuthority == null) {
			return ResultVo.error();
		}
		productAuthority.setCentreId(getUserCentreId());
		if (productAuthorityService.saveCentreIndustry(productAuthority) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	/**
	 * 移除操作
	 * @param id
	 * @return
	 */
	@Log(module = "商品权限管理", operation = "移除商品权限")
	@ResponseBody
	@RequiresPermissions("product:productAuthority:remove")
	@PostMapping("/remove")
	public ResultVo remove(Long id) {
		if (ValidateUtil.isEmptyObjectOrString(id)) {
			return ResultVo.error();
		}
		List<MemberProductAuthority> memberProductAuthorityList = memberProductAuthorityService.selectMemberProductAuthorityListByProductAuthortyId(id);
		if(!CollectionUtils.isEmpty(memberProductAuthorityList)){
			for ( MemberProductAuthority memberProductAuthority : memberProductAuthorityList ) {
				memberProductAuthorityService.removeMemberProductAuthority(memberProductAuthority.getId());
			}
		}
		if (productAuthorityService.removeProductAuthority(id) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}
}
